package com.weilai.common.controller;

import com.alibaba.fastjson.JSON;
import com.weilai.common.model.SysUser;
import com.weilai.common.request.SmsSendRequest;
import com.weilai.common.response.SmsSendResponse;
import com.weilai.common.service.SysUserService;
import com.weilai.common.utils.ChuangLanSmsUtil;
import com.weilai.common.utils.StringRandom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Song on 2017/6/23.
 */
@Controller
@RequestMapping("/sendMessage")
public class SendMessageController {

    @Autowired
    SysUserService sysUserService;

    @RequestMapping(value = "send",method = RequestMethod.POST)
    @ResponseBody
    public SmsSendResponse send(@RequestBody String mobileNumber, HttpServletRequest request){
        // 用户平台API账号(非登录账号,示例:N1234567)
          String account = "N6240241";
        // 用户平台API密码(非登录密码)
          String pswd = "quZPGVRJCA6833";

        //请求地址请登录253云通讯自助通平台查看或者询问您的商务负责人获取
        String smsSingleRequestServerUrl = "http://smssh1.253.com/msg/send/json";
        //产生一个验证码
        String stringRandom = StringRandom.getStringRandom(4);
        // 短信内容
        String msg = "【麦家隆】的用户你好,你的验证码是"+stringRandom;
        //手机号码
        String phone = mobileNumber;
        //状态报告
        String report= "true";
        SmsSendRequest smsSingleRequest = new SmsSendRequest(account, pswd, msg, phone,report);
        String requestJson = JSON.toJSONString(smsSingleRequest);
        System.out.println("before request string is: " + requestJson);
        String response = ChuangLanSmsUtil.sendSmsByPost(smsSingleRequestServerUrl, requestJson);
        System.out.println("response after request result is :" + response);
        SmsSendResponse smsSingleResponse = JSON.parseObject(response, SmsSendResponse.class);
        System.out.println("response  toString is :" + smsSingleResponse);
        request.getSession().setAttribute("stringRandom",stringRandom);


        //添加用户
        SysUser sysUser = new SysUser();
        sysUser.setMobileNumber(mobileNumber);
        sysUser.setName(mobileNumber);
        sysUser.setPwd(mobileNumber);
        sysUser.setUserType("0");
        sysUserService.addSysUser(sysUser);
        return smsSingleResponse;
    }


}
